package 力扣日常刷题.木23一月.第05天305;

import 力扣日常刷题.ListNode;

import java.awt.*;
import java.util.Deque;
import java.util.Stack;

/**
 * @author 帅小伙
 * @date 2022/3/5
 * @description
 */
public class Demo50链表相加 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();

        while (l1 != null) {
            stack1.push(l1.val);
            l1 = l1.next;
        }
        while(l2 != null){
            stack2.push(l2.val);
            l2 = l2.next;
        }
        int c = 0;
        ListNode head = new ListNode(-1);
        while(!stack1.isEmpty() || !stack2.isEmpty() || c != 0) {
            int sum = c;
            sum += stack1.isEmpty() ? 0 : stack1.pop();
            sum += stack2.isEmpty() ? 0 : stack2.pop();
            c = sum / 10;
            ListNode cur = new ListNode(sum % 10);
            // 头插法
            cur.next = head.next;
            head.next = cur;
        }
        return head.next;
    }
}
